# 判断当前操作系统
# 返回系统名字, 例: "windows", "linux", "macos", "unix"
# @return [String]
def host_os()
  os = RbConfig::CONFIG['host_os']
  case os
  when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
    "windows"
  when /darwin|mac os/
    "macos"
  when /linux/
    "linux"
  when /solaris|bsd/
    "unix"
  else
    raise "unknown os: #{os.inspect}"
  end
end

if host_os()=="windows"
	PYTHON_BIN_NAME = "python"
	PIP_BIN_NAME = "pip"
else
	PYTHON_BIN_NAME = "python3"
	PIP_BIN_NAME = "pip3"
end

# app name
APP_NAME = File.basename(__dir__)

desc "make pyz package"
task :dist do
	rm_rf "output/pack_content"
	mkdir_p "output/pack_content"
	rm_rf "output/#{APP_NAME}.pyzw"
	# cp_r "src","output/pack_content/src/" 
	cp_r "main","output/pack_content/main/" 
	if File.exist?("lib")
		cp_r "lib","output/pack_content/lib/"
	end
	cp "__main__.py","output/pack_content/"

	curr_time = Time.now
	output_dir = "output/#{APP_NAME}_V%04d%02d%02d" % [curr_time.year, curr_time.month, curr_time.day]
	rm_rf output_dir
	mkdir_p output_dir
	# mkdir_p output_dir+"/bin"

	sh "#{PYTHON_BIN_NAME} -m zipapp -p \"/usr/bin/env python3\" output/pack_content -o #{output_dir}/#{APP_NAME}.pyzw"
	# cp_r "data","#{output_dir}"
	puts "distribution package created:\n  #{output_dir}"
end

desc "install dependency"
task :install_dependency do
	sh "#{PIP_BIN_NAME} install -r requirements.txt"
end